package com.lolc.ihm.component;

import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JLabel;

import com.lolc.LoLAutoCounterManager;
import com.lolc.bean.Champion;
import com.lolc.bean.Lane;
import com.lolc.ihm.Constants;
import com.lolc.ihm.MainWindow;
import com.lolc.ihm.listener.SaveChampionListener;
import com.lolc.ihm.listener.SaveLaneListener;

public class PlayerBox extends Box{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected JComboBox<ComboBoxItem> championCB;
	protected JComboBox<ComboBoxItem> laneCB;
	protected JLabel iconClearOn;
	protected JLabel iconClearOff;
	protected int id;
	
	
	public PlayerBox(int axis) {
		super(axis);
		// TODO Auto-generated constructor stub
	}
	
	public  PlayerBox(int axis, int id) {
		super(axis);
		this.id=id;
	}
	
	/**
	 * Initializes the combobox with all champions
	 * @param id
	 */
	public void initchampionCB(int id, String group, Champion selected){
		//Champion comboBox
		this.championCB=new JComboBox<ComboBoxItem>();
		this.championCB.setPreferredSize(new Dimension(Constants.COMBOBOX_CHAMPION_WIDTH, Constants.COMBOBOX_HEIGHT));
		//this.championCB.setEditable(true);
		
		List<Champion> champToIgnore =  LoLAutoCounterManager.getInstance().getSelectedChampion();
		for(Champion champion :LoLAutoCounterManager.getInstance().getChampionsList())
		{	
			//Do not insert selected champion	
			if(champToIgnore.contains(champion) && (selected==null || !champion.getId().equals(selected.getId())))
			{
				continue;
			}
			this.championCB.addItem(new ComboBoxItem(champion.getId(), champion.getName()));
		}

		if(selected!=null)
		{
			this.championCB.setSelectedItem(new ComboBoxItem(selected.getId(), selected.getName()));
		}
		else
		{
			this.championCB.setSelectedIndex(-1);
		}
		AutoCompletion.enable(championCB);
		//this.championCB.setEditor(new ComboBoxEditorRenderer());
		this.championCB.addActionListener(new SaveChampionListener(group,id));
		this.championCB.validate();
		this.add(this.championCB);
	}
	
	/**
	 * Initializes the list with all lanes
	 * @param id
	 */
	public void initLaneCB(int id, String group, Lane selected){
		//Role comboBox
		this.laneCB = new JComboBox<ComboBoxItem>();
		this.laneCB.setPreferredSize(new Dimension(Constants.COMBOBOX_LANE_WIDTH, Constants.COMBOBOX_HEIGHT));
		for(Lane lane : LoLAutoCounterManager.getInstance().getLanesList())
		{
			this.laneCB.addItem(new ComboBoxItem(lane.getId(), lane.getLaneName()));
		}		

		this.laneCB.addActionListener(new SaveLaneListener(group,id));
		if(selected!=null)
		{
			this.laneCB.setSelectedItem(new ComboBoxItem(selected.getId(), selected.getLaneName()));
		}
		else
		{
			this.laneCB.setSelectedIndex(-1);
		}
		
		AutoCompletion.enable(this.laneCB);
		
		this.laneCB.validate();
		this.add(this.laneCB);
	}
	
	/**
	 * Initializes button "Clear"
	 * @param id
	 */
	public void initClearBtn()
	{
		//Button activated
		ImageIcon iconImgOn = new ImageIcon(MainWindow.class.getResource(Constants.ICON_REMOVE_ITEM));
		iconClearOn = new JLabel(iconImgOn);
		iconClearOn.setToolTipText(Constants.ICON_REMOVE_ITEM_TOOL_TIP);
		iconClearOn.setVisible(false);
		iconClearOn.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				iconClearOn.setVisible(false);
				iconClearOff.setVisible(true);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				clearPlayerSelection();
			}
		});		
		this.add(iconClearOn);
		
		ImageIcon iconImgff = new ImageIcon(MainWindow.class.getResource(Constants.ICON_REMOVE_ITEM_GREY));
		iconClearOff = new JLabel(iconImgff);
		iconClearOff.setToolTipText(Constants.ICON_REMOVE_ITEM_TOOL_TIP);
		iconClearOff.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				iconClearOff.setVisible(false);
				iconClearOn.setVisible(true);
			}

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
			}
		});	
		this.add(iconClearOff);
	}
	
	/**
	 * Clear selection in comboboxes
	 */
	public void clearPlayerSelection()
	{
		this.championCB.setSelectedItem(null);
		this.laneCB.setSelectedItem(null);
	}
	
	/**
	 * Remove all components
	 */
	public void resetBox()
	{		
		this.removeAll();
	}
			
	/**
	 * @return the championCB
	 */
	public JComboBox<ComboBoxItem> getChampionCB() {
		return championCB;
	}
	/**
	 * @param championCB the championCB to set
	 */
	public void setChampionCB(JComboBox<ComboBoxItem> championCB) {
		this.championCB = championCB;
	}
	/**
	 * @return the laneCB
	 */
	public JComboBox<ComboBoxItem> getLaneCB() {
		return laneCB;
	}
	/**
	 * @param laneCB the laneCB to set
	 */
	public void setLaneCB(JComboBox<ComboBoxItem> laneCB) {
		this.laneCB = laneCB;
	}
	/**
	 * @return the iconClearOn
	 */
	public JLabel getIconClearOn() {
		return iconClearOn;
	}
	/**
	 * @param iconClearOn the iconClearOn to set
	 */
	public void setIconClearOn(JLabel iconClearOn) {
		this.iconClearOn = iconClearOn;
	}
	/**
	 * @return the iconClearOff
	 */
	public JLabel getIconClearOff() {
		return iconClearOff;
	}
	/**
	 * @param iconClearOff the iconClearOff to set
	 */
	public void setIconClearOff(JLabel iconClearOff) {
		this.iconClearOff = iconClearOff;
	}
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	
	
}
